using System;
using System.Collections;

using DesignByContract;

namespace Outcoding.GraphGenerator.Entities {
    public class ShapeCollection : CollectionBase {

        /// <summary>
        /// Default Constructor
        /// </summary>
        public ShapeCollection() {
        }

        /// <summary>
        /// This index into Shape Collection.
        /// </summary>
        /// <param name="index">The index of the Shape.</param>
        /// <returns>A Shape object located in the given index.</returns>
        public Shape this[int index] {
            get {
                return (Shape)List[index];
            }
            set {
                List[index] = value;
            }
        }

        /// <summary>
        /// Add new Shape to this collection.
        /// </summary>
        /// <param name="value">Shape to add.</param>
        /// <returns>Index of the new value added.</returns>
        public int Add(Shape value) {
            return List.Add(value);
        }

        /// <summary>
        /// Find the index for a given Shape.
        /// </summary>
        /// <param name="value">Shape to look for.</param>
        /// <returns>The index assign to the Shape object.</returns>
        public int IndexOf(Shape value) {
            return List.IndexOf(value);
        }

        /// <summary>
        /// Add new Shape to this collection in the specified index.
        /// </summary>
        /// <param name="index">The index to place the Shape.</param>
        /// <param name="value">The Shape object to add.</param>
        public void Insert(int index, Shape value) {
            List.Insert(index, value);
        }

        /// <summary>
        /// Remove a Shape from this collection.
        /// </summary>
        /// <param name="value">Shape object to remove.</param>
        public void Remove(Shape value) {
            List.Remove(value);
        }

        /// <summary>
        /// Indicates if a Shape exists in this collection.
        /// </summary>
        /// <param name="value">The Shape to search.</param>
        /// <returns>A Boolean value indicating whether or not the Shape exists in this collection.</returns>
        public bool Contains(Shape value) {
            // If value is not of type Shape, this will return false.
            return List.Contains(value);
        }

        /// <summary>
        /// Check Type on insert.
        /// </summary>
        protected override void OnInsert(int index, object value) {
            base.OnInsert(index, value);
            if (value.GetType() != typeof(Shape))
                throw new ArgumentException("value must be of type Shape", "value");
        }

        /// <summary>
        /// Check type on removal.
        /// </summary>
        protected override void OnRemove(int index, object value) {
            if (value.GetType() != typeof(Shape))
                throw new ArgumentException("value must be of type Shape.", "value");
        }

        /// <summary>
        /// Check type on set.
        /// </summary>
        protected override void OnSet(int index, object oldValue, object newValue) {
            if (newValue.GetType() != typeof(Shape))
                throw new ArgumentException("newValue must be of type Shape.", "newValue");
        }

        /// <summary>
        /// Check type on validate.
        /// </summary>
        protected override void OnValidate(object value) {
            if (value.GetType() != typeof(Shape))
                throw new ArgumentException("value must be of type Shape.");
        }
    }
}